package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SOrderItemMapper;
import com.yuelife.service.pojo.SOrderItem;
import com.yuelife.service.service.SOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SOrderItem业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SOrderItemServiceImpl implements SOrderItemService {

    @Autowired
    private SOrderItemMapper sOrderItemMapper;


    /**
     * SOrderItem条件+分页查询
     * @param sOrderItem 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SOrderItem> findPage(SOrderItem sOrderItem, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sOrderItem);
        //执行搜索
        return new PageInfo<SOrderItem>(sOrderItemMapper.selectByExample(example));
    }

    /**
     * SOrderItem分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SOrderItem> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SOrderItem>(sOrderItemMapper.selectAll());
    }

    /**
     * SOrderItem条件查询
     * @param sOrderItem
     * @return
     */
    @Override
    public List<SOrderItem> findList(SOrderItem sOrderItem){
        //构建查询条件
        Example example = createExample(sOrderItem);
        //根据构建的条件查询数据
        return sOrderItemMapper.selectByExample(example);
    }


    /**
     * SOrderItem构建查询对象
     * @param sOrderItem
     * @return
     */
    public Example createExample(SOrderItem sOrderItem){
        Example example=new Example(SOrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(sOrderItem!=null){
            // 订单明细id
            if(!StringUtils.isEmpty(sOrderItem.getItemId())){
                    criteria.andEqualTo("itemId",sOrderItem.getItemId());
            }
            // 订单id
            if(!StringUtils.isEmpty(sOrderItem.getOrderId())){
                    criteria.andEqualTo("orderId",sOrderItem.getOrderId());
            }
            // 服务商品id
            if(!StringUtils.isEmpty(sOrderItem.getServiceId())){
                    criteria.andEqualTo("serviceId",sOrderItem.getServiceId());
            }
            // 商品名称
            if(!StringUtils.isEmpty(sOrderItem.getServiceName())){
                    criteria.andEqualTo("serviceName",sOrderItem.getServiceName());
            }
            // 商品价格
            if(!StringUtils.isEmpty(sOrderItem.getServicePrice())){
                    criteria.andEqualTo("servicePrice",sOrderItem.getServicePrice());
            }
            // 
            if(!StringUtils.isEmpty(sOrderItem.getPromotionPrice())){
                    criteria.andEqualTo("promotionPrice",sOrderItem.getPromotionPrice());
            }
            // 
            if(!StringUtils.isEmpty(sOrderItem.getDescription())){
                    criteria.andEqualTo("description",sOrderItem.getDescription());
            }
            // 
            if(!StringUtils.isEmpty(sOrderItem.getImg())){
                    criteria.andEqualTo("img",sOrderItem.getImg());
            }
            // 购买数量
            if(!StringUtils.isEmpty(sOrderItem.getServiceQuantity())){
                    criteria.andEqualTo("serviceQuantity",sOrderItem.getServiceQuantity());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sOrderItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SOrderItem
     * @param sOrderItem
     */
    @Override
    public void update(SOrderItem sOrderItem){
        sOrderItemMapper.updateByPrimaryKey(sOrderItem);
    }

    /**
     * 增加SOrderItem
     * @param sOrderItem
     */
    @Override
    public void add(SOrderItem sOrderItem){
        sOrderItemMapper.insert(sOrderItem);
    }

    /**
     * 根据ID查询SOrderItem
     * @param id
     * @return
     */
    @Override
    public SOrderItem findById(Integer id){
        return  sOrderItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SOrderItem全部数据
     * @return
     */
    @Override
    public List<SOrderItem> findAll() {
        return sOrderItemMapper.selectAll();
    }

    /**
     * 根据订单id查订单明细
     * @param orderId
     * @return
     */
    @Override
    public List<SOrderItem> fnidSOrderItemBySOrderId(Integer orderId) {
        return sOrderItemMapper.fnidSOrderItemBySOrderId(orderId);
    }
}
